package com.gin.no_gay_alliance.fragment.post.emote;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gin.no_gay_alliance.adpter.emote.EmoteItemAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.databinding.FragmentEmotePageBinding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.utils.HandlerMessage;
import com.gin.no_gay_alliance.viewmodel.InsertText;
import com.gin.no_gay_alliance.viewmodel.emote.EmoteModel;

import java.util.ArrayList;

/**
 * 一页表情
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/27 20:20
 **/
public class EmotePageFragment extends BaseBindingFragment<FragmentEmotePageBinding> implements View.OnClickListener {
    public static final String TAG = EmotePageFragment.class.getSimpleName();
    ArrayList<EmoteModel> data;

    @Override
    protected void onCreateView() {
        Bundle arguments = getArguments();
        if (arguments == null) {
            throw new RequireParamException();
        }
        data = arguments.getParcelableArrayList(EmoteModel.class.getName());
        b.recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = (int) (30 * MyApplication.DEVICE_DENSITY_DPI / 160f);
            }
        });
        b.recyclerView.setAdapter(new EmoteItemAdapter(requireActivity(), data, this));
    }


    @Override
    public void onClick(View v) {
        String code = v.getTag().toString();
        // 插入表情
        getParentHandler().sendMessage(getParentHandler().obtainMessage(HandlerMessage.INSERT_TEXT, new InsertText(code)));
        // 记录使用次数
        MyApplication.app.managerFactory.getEmoteManager().count(code);
    }
}
